package com.cx.controller;

import com.cx.pojo.Concern;
import com.cx.pojo.Post;
import com.cx.pojo.User;
import com.cx.service.ConcernService;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("concern")
public class ConcernController {

    @Autowired
    ConcernService concernService;
    @Autowired
    UserCenterService userCenterService;


    @RequestMapping("selectByConcerningId")
    @ResponseBody
    public List<Concern> selectByConcerningId(Integer concerningId){
        List<Concern> concernList = concernService.selectByConcerningId(concerningId);
        return concernList;
    }

    @RequestMapping("selectByConcernedId")
    @ResponseBody
    public List<Concern> selectByConcernedId(Integer concernedId){
        List<Concern> concernedList = concernService.selectByConcernedId(concernedId);
        return concernedList;
    }


    @RequestMapping("deleteConcern")
    @ResponseBody
    public String deleteConcern(String userName, String userByName){
        Integer rows = concernService.deleteConcern(userName,userByName);
        if(rows > 0){
            Integer row = userCenterService.updateConcernDel(userName);
            Integer row1 = userCenterService.updateFansDel(userByName);
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping("insertConcern")
    @ResponseBody//我的名字，我要关注的名字
    public String insertConcern(String userName,String userByName){
        Concern concern = new Concern();
        concern.setOther1(userName);
        concern.setOther2(userByName);
        Integer rows = concernService.insertConcern(concern);
        if(rows > 0){
            Integer row = userCenterService.updateConcernAdd(userName);
            Integer row1 = userCenterService.updateFansAdd(userByName);
            return "success";
        }else {
            return "fail";
        }
    }


    @RequestMapping("selectByOther1Name")
    @ResponseBody
    public List<User> selectByOther1Name(String userName){
        List<Concern> concerns = concernService.selectByOther1Name(userName);
        List<User> users = new ArrayList<>();
        for (int i = 0; i < concerns.size(); i++){
            users.add(userCenterService.selectByUsetName(concerns.get(i).getOther2()));
        }

        return users;

    }

    @RequestMapping("selectByOther2Name")
    @ResponseBody
    public List<User> selectByOther2Name(String userName){
        List<Concern> concerns = concernService.selectByOther2Name(userName);
        List<User> users = new ArrayList<>();
        for (int i = 0; i < concerns.size(); i++){
            users.add(userCenterService.selectByUsetName(concerns.get(i).getOther1()));
        }

        return users;

    }


    // 判断用户是否关注了某个人
    @RequestMapping("selectByUserNameAndUserByName")
    @ResponseBody
    public Integer selectByUserNameAndUserByName(String userName,String userByName){
        List<Concern> concerns = concernService.selectByUserNameAndUserByName(userName,userByName);
        if (concerns.isEmpty()){
            return 0;
        }else {
            return 1;
        }

    }


}
